package io.renren.common.utils;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;

public class StringUtils {

    public static void checkImgurl(String imgurl) {
        if (isBlank(imgurl)) {
            return;
        }
        if (imgurl.contains(",")) {
            String[] split = imgurl.split(",");
            for (String s : split) {
                if (s.contains("http:") || s.contains("https://")) {
                    throw new RuntimeException("图片格式异常");
                }
            }
        } else {
            if (imgurl.contains("http:") || imgurl.contains("https://")) {
                throw new RuntimeException("图片格式异常");
            }
        }
    }

    public static boolean isBlank(String str) {
        return str == null || str.length() == 0;
    }


    public static boolean isNotBlank(String str) {
        return !StringUtils.isBlank(str);
    }


    public static String getUUID() {
        UUID uuid = UUID.randomUUID();
        return uuid.toString().toLowerCase().replace("-", "");
    }

    public static boolean isNum(String value) {
        try {
            double d = Double.parseDouble(value);
            return d > 0;
        } catch (Exception e) {
        }
        return false;
    }

    public static boolean isEmail(String email) {
        if (email == null)
            return false;
        if (email.length() > 50)
            return false;
        return email.matches("\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*");
    }

    public static boolean isMobile(String value) {
        if (value == null)
            return false;
        if (!value.startsWith("1")) {
            return false;
        }
        return value.length() == 11;
    }

    public static double d2(double d) {
        BigDecimal bg = new BigDecimal(d);
        double f1 = bg.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
        return f1;
    }

    public static String createOrdernum(long uid, String type) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
        String ordernum = "U" + uid + sdf.format(new Date()) + type;
        return ordernum;
    }

    public static String nickname(String mobile) {
        mobile = mobile.replace(mobile.substring(3, mobile.length()), "********");
        return mobile;
    }

    public static String array2String(List<String> array) {
        StringBuilder sb = new StringBuilder();
        for (String d : array) {
            if (sb.length() > 0) {
                sb.append(",");
            }
            sb.append(d);
        }
        return sb.toString();
    }

    public static double d1(double d) {
        BigDecimal bg = new BigDecimal(d);
        double f1 = bg.setScale(1, BigDecimal.ROUND_HALF_UP).doubleValue();
        return f1;
    }
}
